package bb.lanxing.util.ui;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class StatusBarUtil {
    public static void immersive(Activity activity, int i) {
        immersive(activity.getWindow(), i);
    }

    public static void immersive(Window window, int i) {
        if (Build.VERSION.SDK_INT >= 21) {
            window.clearFlags(67108864);
            window.addFlags(Integer.MIN_VALUE);
            window.setStatusBarColor(i);
            window.getDecorView().setSystemUiVisibility(window.getDecorView().getSystemUiVisibility() | 1024 | 256);
        } else if (Build.VERSION.SDK_INT >= 19) {
            window.addFlags(67108864);
            setTranslucentView((ViewGroup) window.getDecorView(), i);
        } else if (Build.VERSION.SDK_INT < 19 || Build.VERSION.SDK_INT <= 16) {
        } else {
            window.getDecorView().setSystemUiVisibility(window.getDecorView().getSystemUiVisibility() | 1024 | 256);
        }
    }

    public static boolean darkMode(Activity activity, boolean z) {
        if (isFlyme()) {
            darkModeForFlyme4(activity.getWindow(), z);
            darkModeForM(activity.getWindow(), z);
            return true;
        } else if (isMIUI6Later()) {
            darkModeForMIUI6(activity.getWindow(), z);
            darkModeForM(activity.getWindow(), z);
            return true;
        } else {
            darkModeForM(activity.getWindow(), z);
            return true;
        }
    }

    public static void shadowStatusBar(Activity activity, boolean z) {
        Window window = activity.getWindow();
        if (z) {
            window.addFlags(67108864);
        } else {
            window.clearFlags(67108864);
        }
    }

    private static void darkModeForM(Window window, boolean z) {
        int systemUiVisibility = window.getDecorView().getSystemUiVisibility();
        window.getDecorView().setSystemUiVisibility(z ? systemUiVisibility | 8192 : systemUiVisibility & (-8193));
    }

    private static boolean darkModeForFlyme4(Window window, boolean z) {
        if (window != null) {
            try {
                WindowManager.LayoutParams attributes = window.getAttributes();
                Field declaredField = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field declaredField2 = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
                declaredField.setAccessible(true);
                declaredField2.setAccessible(true);
                int i = declaredField.getInt(null);
                int i2 = declaredField2.getInt(attributes);
                declaredField2.setInt(attributes, z ? i2 | i : (~i) & i2);
                window.setAttributes(attributes);
                return true;
            } catch (Exception unused) {
                Log.e("StatusBar", "darkIcon: failed");
            }
        }
        return false;
    }

    private static boolean darkModeForMIUI6(Window window, boolean z) {
        Class<?> cls = window.getClass();
        try {
            Class<?> cls2 = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            int i = cls2.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE").getInt(cls2);
            Method method = cls.getMethod("setExtraFlags", Integer.TYPE, Integer.TYPE);
            Object[] objArr = new Object[2];
            objArr[0] = z ? i : 0;
            objArr[1] = i;
            method.invoke(window, objArr);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean isFlyme() {
        try {
            return Build.class.getMethod("hasSmartBar", new Class[0]) != null;
        } catch (Exception unused) {
            return false;
        }
    }

    private static boolean isMIUI6Later() {
        try {
            return Integer.parseInt(((String) Class.forName("android.os.SystemProperties").getMethod("get", String.class).invoke(null, "ro.miui.ui.version.name")).replaceAll("[vV]", "")) >= 6;
        } catch (Exception unused) {
            return false;
        }
    }

    public static void setPadding(Context context, View view) {
        if (Build.VERSION.SDK_INT >= 19) {
            view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(context), view.getPaddingRight(), view.getPaddingBottom());
        }
    }

    public static void setPaddingSmart(Context context, View view) {
        if (Build.VERSION.SDK_INT >= 19) {
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            if (layoutParams != null && layoutParams.height > 0) {
                layoutParams.height += getStatusBarHeight(context);
            }
            view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(context), view.getPaddingRight(), view.getPaddingBottom());
        }
    }

    public static void setTranslucentView(ViewGroup viewGroup, int i) {
        if (Build.VERSION.SDK_INT >= 19) {
            View findViewById = viewGroup.findViewById(android.R.id.custom);
            if (findViewById == null && i != 0) {
                findViewById = new View(viewGroup.getContext());
                findViewById.setId(android.R.id.custom);
                viewGroup.addView(findViewById, new ViewGroup.LayoutParams(-1, getStatusBarHeight(viewGroup.getContext())));
            }
            if (findViewById != null) {
                findViewById.setBackgroundColor(i);
            }
        }
    }

    public static int getStatusBarHeight(Context context) {
        int identifier = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (identifier > 0) {
            return context.getResources().getDimensionPixelSize(identifier);
        }
        return (int) TypedValue.applyDimension(1, 24, Resources.getSystem().getDisplayMetrics());
    }
}